package session;

import game.Game;
import game.screens.GameScreen;
import game.screens.WaitingScreen;
import shared.Message;
import shared.io.game.EndGame;
import shared.io.game.GameState;
import shared.io.game.Player;
import shared.io.messages.ErrorMessage;
import shared.io.startup.ClientStartup;
import shared.io.startup.NewGame;
import shared.io.startup.Waiting;

/**
 * Manages incoming java.io.Serializable objects
 */
public class ObjectManager {
   
   private Game game;

   public ObjectManager(Game game) {
      this.game = game;
   }
   
   /**
    * Manages incoming objects.
    * Object must implement java.io.Serializable and be known to the client.
    */
   public void manageObject(Object object) {
      if (object instanceof java.io.Serializable) {
         if (object instanceof GameState) {
            GameState state = (GameState) object;
            game.setState(state);
            return;
         } else if (object instanceof Player) {
            game.addPlayer((Player) object);
            return;
         } else if (object instanceof ClientStartup) {
            ClientStartup startup = (ClientStartup) object;
            game.addPlayers(startup.getPlayers());
            game.setMaze(startup.getMaze());
            game.setPlayer(startup.getPlayer());
            return;
         } else if (object instanceof NewGame) {
            NewGame newGame = (NewGame) object;
            game.getPlayer().setLoc(newGame.getState().getPlayers().get(newGame.getState()
                  .getPlayers().indexOf(game.getPlayer())).getLoc());
            game.setMaze(newGame.getMaze());
            game.setState(newGame.getState());
         } else if (object instanceof Message) {
            game.addMessage((Message) object);
            return;
         } else if (object instanceof ErrorMessage) {
            System.out.println("[ <" + ((ErrorMessage)object).getMessage() + "> ]");
         } else if (object instanceof EndGame) {
            EndGame end = (EndGame) object;
            game.setEndGame(end);
         } else if (object instanceof Waiting) {
            game.setScreen(WaitingScreen.ID);
         }
         
      }
      
   }
   
}
